
const app = getApp()
var HttpUtil = require('../../utils/httpUtil.js');


Page({
  data: {
    //判断小程序的API，回调，参数，组件等是否在当前版本可用。
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    isHide: false,
    isShow:true,
    userInfo: app.globalData.userInfo,
    myinfo: null,
    gender: ['女', '男'],
    ageGroup: ['0', '0.7', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13-15', '16-17', '18-44', '45-49', '50-59', '60-69', '70-79', '80-100', '孕妇(早)', '孕妇(中)', '乳母'],
    height: ['155cm以下','155-160cm', '160-165cm', '165-170cm', '170-175cm', '175-180cm', '180-185cm', '185-190cm','190cm以上'],
    weight: ['40kg以下','40-50kg', '50-60kg', '60-70kg', '70-80kg', '80-90kg', '90-100kg','100kg以上'],
    strength: [],
    
    genderIndex: 1,
    ageGroupIndex: 1,
    heightIndex: 3,
    weightIndex: 2,
    strengthIndex:0,

    ageGroupMap:[],
    heightMap: [],
    weightMap: [],
    strengthMap: [],
    //submitUser:{}


  },

  onLoad: function (options) {
    var stu = wx.getStorageSync('student');
    this.setData({ myinfo: stu });

    //开始年龄组
    let ageList = this.data.ageGroup;
    let ageGroupMap=[]
    for (let index in ageList) {
      ageGroupMap[ageList[index]] = index;
    }
    this.data.ageGroupMap = ageGroupMap;
    console.log("可用年龄组", this.data.ageGroupMap)

    //开始身高
    let heightList = this.data.height;
    let heightMap = []
    for (let index in heightList) {
      heightMap[heightList[index]] = index;
    }
    this.data.heightMap = heightMap;
    console.log("可用身高", this.data.heightMap)

    //开始体重
    let weightList = this.data.weight;
    let weightMap = []
    for (let index in weightList) {
      weightMap[weightList[index]] = index;
    }
    this.data.weightMap =weightMap;
    console.log("可用体重", this.data.weightMap)
    




    //工作强度
    // let strengthList = ['极轻', '轻', '中', '重', '极重','-'];
    // let strengthMap = []
    // for (let index in strengthList) {
    //   strengthMap[strengthList[index]] = index;
    // }
    // this.data.strengthMap = strengthMap;
    // console.log("可用工作强度", this.data.strengthMap)
  },
  

  
  exit: function (e) {
    wx.showModal({
      title: '提示',
      content: '是否确认退出',
      success: function (res) {
        if (res.confirm) {
          // console.log('用户点击确定')
          wx.removeStorageSync('student');
          //页面跳转
          wx.redirectTo({
            url: '../canting/canting',
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  
  onShow: function () {
    var that = this;
    // 查看是否授权
    wx.getSetting({
      success: function (res) {
        if (res.authSetting['scope.userInfo']) {
           that.setData({
             isShow: false,
             
           })
           
        } else {
          // 用户没有授权
          that.setData({
            isHide: true
          });
          return
        }
      }
    });
  //  app.initUserInfo();
    let user= app.globalData.userInfo;
    // let submitUser = this.data.submitUser;
    // submitUser.userId = user.userId
    // submitUser.gender=user.gender
    // submitUser.height = user.height
    // submitUser.weight=user.weight
    // submitUser.ageGroup=user.ageGroup
    // submitUser.strength=user.strength
    // this.data.submitUser = submitUser
    // console.log("将要修改资料用户", this.data.submitUser)
    let ageGroupIndex = this.data.ageGroupMap[user.ageGroup]
    let weightIndex = this.data.weightMap[user.weight]
    let heightIndex = this.data.heightMap[user.height]
     
    this.commonChangeStrength(user.ageGroup)
    let strengthIndex = this.data.strengthMap[user.strength]
    console.log("初始用户年龄", user.ageGroup, "强度", strengthIndex)
    this.setData({
      userInfo: user,
      genderIndex: user.gender,
      ageGroupIndex: ageGroupIndex,
      heightIndex: heightIndex,
      weightIndex: weightIndex,
      strengthIndex: strengthIndex,
    });
  },

  modifyUser: function () {
    let that=this

    wx.showModal({ //提交修改提示框
      title: '个人信息',
      content: '确定修改？',//修改提示框标题
      success: function (res) {
          let gender= that.data.genderIndex
          let ageGroup= that.data.ageGroup[that.data.ageGroupIndex]
           let strength= that.data.strength[that.data.strengthIndex]
           let height= that.data.height[that.data.heightIndex]
        let weight= that.data.weight[that.data.weightIndex]

        let submitData = {
          userId: app.globalData.userInfo.userId,
          gender: gender,
          ageGroup: ageGroup,
          strength: strength,
          height: height,
          weight: weight,
        }


        var url = app.globalData.serverPath + '/basicUser/updateUser';
        HttpUtil.POSTJSON(url, submitData).then((res) => {
          let userInfo=app.globalData.userInfo
          var standardIntake = res.data.result
          userInfo.standardIntake = standardIntake
          userInfo.gender = gender
          userInfo.ageGroup = ageGroup
          userInfo.strength = strength
          userInfo.height = height
          userInfo.weight = weight
          
          app.globalData.userInfo=userInfo
          wx.setStorageSync('userInfo', userInfo)
          console.log("修改后user", app.globalData.userInfo)
          wx.showToast({
            title: '摄入标准已调整',
            icon: 'success',
            duration: 2000
          })
        }).catch((errMsg) => {
          console.log("查询使用者信息失败", errMsg);
        });

      }
    })

  },
  commonChangeStrength: function (ageGroupValue) {
    var strength=['-']
    if (ageGroupValue == "18-44") {
      strength = ['极轻', '轻', '中', '重', '极重']
    }
    if (ageGroupValue == "45-49") {
      strength = ['极轻', '轻', '中', '重']
    }
    if (ageGroupValue == "50-59") {
      strength = ['轻', '中', '重']
    }
    if (ageGroupValue == "60-69") {
      strength = ['极轻', '轻', '中']
    }
    if (ageGroupValue == "70-79") {
      strength = ['极轻', '轻']
    }
     let strengthMap = []
    for (let index in strength) {
      strengthMap[strength[index]] = index;
    }
    this.data.strengthMap = strengthMap;
   
   
    if (ageGroupValue == "孕妇(早)" || ageGroupValue == "孕妇(中)" || ageGroupValue == "乳母") {
      this.setData({
        genderIndex: 0
      })
    }
   
    this.setData({
      strength: strength,

    })

  },
  bindGenderChange: function (e) {
    console.log('性别发送选择改变，携带值为', e.detail.value)
    this.setData({
      genderIndex: e.detail.value
    })
  },
  bindAgeGroupChange: function (e) {
    console.log('AgeGroup发送选择改变，携带值为', e)
    let ageGroupValue = this.data.ageGroup[e.detail.value];
    console.log('AgeGroup发送选择改变', ageGroupValue)
    this.commonChangeStrength(ageGroupValue)
    this.setData({
      ageGroupIndex: e.detail.value,
      strengthIndex: 0
    })
  },
  bindHeightChange: function (e) {
    console.log('身高发送选择改变，携带值为', e.detail.value)
    this.setData({
      heightIndex: e.detail.value
    })
  },
  bindWeightChange: function (e) {
    console.log('体重发送选择改变，携带值为', e.detail.value)
    this.setData({
      weightIndex: e.detail.value
    })
  },
 
  bindStrengthChange: function (e) {
    console.log('强度发送选择改变，携带值为', e.detail.value)
    this.setData({
      strengthIndex: e.detail.value
    })
  },
  
  onShareAppMessage: function () {
    var that = this;
    var data = {
      title: '食品营养与人类健康高精尖创新中心',
      success: (res) => {
        console.debug("转发成功", res);
      },
      fail: (res) => {
        console.debug("转发失败", res);
      }
    }
    return data;
  },
  getUserInfo: function (e) {
    console.log("lllllllllllllllllll"+e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  dl: function () {
    //var that = this
    //app.globalData.userInfo = e.detail.userInfo
    // if (this.userInfo != null && this.userInfo != "") {
    // //  console.log(qqqqqqqqqqq)
    //   this.setData({
      
    //     isShow: false
    //   })
     
    // }else{
    //   this.setData({
    //     isShow: true
    //   })
    // }
    wx.navigateTo({
      url: '/pages/about/authorize/authorize',
    })
    
  
    
    
  },
})
  

    